/**
 * Created by Administrator on 2018-07-09.
 */
define(function (require,exports,module) {
    var sTpl = require("./purchasePlan.html");
    var VueComponent = Vue.extend({
        template: sTpl,
        data:function () {
            return {
                dialogFormVisible:false,
                tableData: {},
                title:'',
                title1:'',
                planInFormInline:{
                    purchasePlanNo:'',
                    purchasingType:'',
                    goodsCode:'',
                    goodsName:'',
                    goodsSpec:'',
                    userDept:'',
                    needDate:'',
                    needNum:'',
                    budgetPrice:'',
                    budgetMoney:'',
                    remarks:'',
                    state:'',
                    creater:''
                },
                checkFormInline:{
                    goodsCode:'',
                    needDate:'',
                    userDept:''
                },
                pickerOptions1: {
                    shortcuts: [{
                        text: '今天',
                        onClick:function(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick:function(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick:function (picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                }
                ,
                pickerOptions2: {
                    shortcuts: [{
                        text: '今天',
                        onClick:function(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick:function(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick:function (picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                }
            }
        },
        methods:{
            pickTime1:function (val) {
                this.planInFormInline.needDate = val;
            },
            pickTime2:function (val) {
                this. checkFormInline.needDate = val;
            },

            findPurchasePlanPo:function () {
                var $this = this;
                axios.get(basepath+"/api/purchaseMgr/findPurchasePlanPo"
                ).then(function (response) {
                    $this.tableData=response.data;
                });
            },
            addPlan:function () {
                this.$refs.baseTable.dialogFormVisible = true;
                this.title='保存成功';
                this.title1='保存失败';
            },
            savePlan:function () {
                var $this = this;
                this.$refs.planInFormInline.validate(function(validate){
                    if(validate){
                        axios.post(basepath+'/api/purchaseMgr/savePurchasePlanPo',$this.planInFormInline)
                            .then(function (response) {
                                $this.$refs["planInFormInline"].resetFields();
                                $this.$notify({
                                    title: $this.title,
                                    message: $this.title
                                });

                            });
                        $this.findPurchasePlanPo();
                        $this.$refs.baseTable.dialogFormVisible = false;

                    }else{
                        $this.$notify({
                            title: $this.title1,
                            message: $this.title1
                        });
                        return  false;
                    }
                });

            },
            clearPlan:function () {
                this.$refs["planInFormInline"].resetFields();
                this.$refs.baseTable.dialogFormVisible = false;
            },
            editPlan:function (scope) {
                    this.$refs.baseTable.dialogFormVisible = true;
                    this.planInFormInline=JSON.parse(JSON.stringify(scope.row));
                    this.title='修改成功';
                    this.title1='修改失败';
            },
            deletePlan:function (scope) {
                    var $this = this ;
                    this.$confirm('确定删除？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'success'
                    }).then(function() {
                        axios.post(basepath+'/api/purchaseMgr/delectPurchasePlanPo',scope.row)
                            .then(function (response) {
                                $this.$notify({
                                    title: '已删除',
                                    message: '已删除'
                                });

                                $this.findPurchasePlanPo();
                            });
                    })
            },
            checkPurchasePlan:function () {
                var $this = this;
                axios.get(basepath+"/api/purchaseMgr/checkPurchasePlanPo",{
                    params: {
                        goodsCode:$this.checkFormInline.goodsCode,
                        needDate:$this.checkFormInline.needDate,
                        userDept:$this.checkFormInline.userDept
                    }}
                ).then(function (response) {
                    console.log(response);
                    $this.tableData=response.data;
                });
            }
        },
        watch:{
        },
        created:function () {
            this.findPurchasePlanPo();

        },
        activated:function () {
            messages.$emit('change','采购计划');
        }

    })
    module.exports=VueComponent;
    module.exports.reDefine=function (val) {
        module.exports.type=val;
        return module.exports;
    }

})

